package com.stars.easyms.alarm;

import com.stars.easyms.alarm.client.EasyMsAlarmClient;
import com.stars.easyms.alarm.message.EasyMsExceptionAlarmMessage;
import com.stars.easyms.alarm.message.EasyMsNormalAlarmMessage;
import com.stars.easyms.alarm.properties.EasyMsAlarmProperties;
import com.stars.easyms.alarm.service.EasyMsAsynAlarmService;
import com.stars.easyms.alarm.util.EasyMsAlarmUtil;

import java.util.List;

/**
 * <p>className: EasyMsAlarmAsynchronousTask</p>
 * <p>description: 告警发送异步类</p>
 *
 * @author caotieshuan
 * @version 1.5.0
 * @date 2019-12-18 16:37
 */
public class EasyMsAlarmService {

    private final EasyMsAlarmProperties alarmProperties;

    private final List<EasyMsAlarmClient> alarmClientList;

    private final AlarmInterceptor alarmInterceptor;

    private final EasyMsAsynAlarmService easyMsAsynAlarmService;

    public EasyMsAlarmService(EasyMsAlarmProperties alarmProperties, List<EasyMsAlarmClient> alarmClientList,
                                    EasyMsAsynAlarmService easyMsAsynAlarmService, AlarmInterceptor alarmInterceptor) {
        this.alarmProperties = alarmProperties;
        this.alarmInterceptor = alarmInterceptor;
        this.alarmClientList = alarmClientList;
        this.easyMsAsynAlarmService = easyMsAsynAlarmService;
    }

    public boolean canAlarm() {
        return alarmInterceptor != null || (alarmProperties.isEnabled() && !alarmClientList.isEmpty());
    }

    public void sendExceptionAlarmMessage(EasyMsExceptionAlarmMessage exceptionAlarmMessage) {
        // 为了防止重复发送，当本地可以告警的情况下不再发送到拦截器中
        if (alarmProperties.isEnabled() && !alarmClientList.isEmpty()) {
            easyMsAsynAlarmService.add(exceptionAlarmMessage);
        } else if (alarmInterceptor != null) {
            alarmInterceptor.sendExceptionAlarmMessage(exceptionAlarmMessage);
        }
    }

    public void sendNormalAlarmMessage(EasyMsNormalAlarmMessage normalAlarmMessage) {
        // 为了防止重复发送，当本地可以告警的情况下不再发送到拦截器中
        if (alarmProperties.isEnabled() && !alarmClientList.isEmpty()) {
            easyMsAsynAlarmService.add(normalAlarmMessage);
        } else if (alarmInterceptor != null) {
            alarmInterceptor.sendNormalAlarmMessage(normalAlarmMessage);
        }
    }

    public void sendExceptionAlarmMessage(Throwable throwable) {
        boolean isHasAlarmInterceptor = alarmInterceptor != null;
        boolean isEnableLocalAlarm = alarmProperties.isEnabled() && !alarmClientList.isEmpty();
        if (isHasAlarmInterceptor || isEnableLocalAlarm) {
            EasyMsExceptionAlarmMessage exceptionAlarmMessage = EasyMsAlarmUtil.getExceptionAlarmMessage(throwable);
            // 为了防止重复发送，当本地可以告警的情况下不再发送到拦截器中
            if (isEnableLocalAlarm) {
                easyMsAsynAlarmService.add(exceptionAlarmMessage);
            } else {
                alarmInterceptor.sendExceptionAlarmMessage(exceptionAlarmMessage);
            }
        }
    }

    public void sendAlarmMessage(String message, Object... args) {
        boolean isHasAlarmInterceptor = alarmInterceptor != null;
        boolean isEnableLocalAlarm = alarmProperties.isEnabled() && !alarmClientList.isEmpty();
        if (isHasAlarmInterceptor || isEnableLocalAlarm) {
            EasyMsNormalAlarmMessage normalAlarmMessage = EasyMsAlarmUtil.getNormalAlarmMessage(message, args);
            // 为了防止重复发送，当本地可以告警的情况下不再发送到拦截器中
            if (isEnableLocalAlarm) {
                easyMsAsynAlarmService.add(normalAlarmMessage);
            } else {
                alarmInterceptor.sendNormalAlarmMessage(normalAlarmMessage);
            }
        }
    }

}